package com.rota.controller;

import com.rota.domain.UserInfo;
import com.rota.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping(value = "/login")
    public String login() {

        return "login";
    }

    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    public String userLogin(@RequestParam String username, @RequestParam String password, Model model, HttpSession session) {

        UserInfo user =  userInfoService.findUser(username,password);
        if (user != null) {
            // 将用户添加到Session
            session.setAttribute("USER_SESSION",user);
            return "redirect:/";
        }
        model.addAttribute("msg","账号或密码错误，请重新输入!");

        return "login";
    }
}
